package cz.cvut.liy;

import cz.cvut.liy.data.Question;
import cz.cvut.liy.data.Test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity{
	
	private Button btnShowAnswer;
	private Button btnNo;
	private Button btnYes;
	private TextView txtName;
	private TextView txtQuestionHead;
	private TextView txtQuestion;
	private TextView txtAnswerHead;
	private TextView txtAnswer;
	private TextView txtAnsweredQuestion;
	
	private Test test;
	private Question question = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        
        btnShowAnswer = (Button)findViewById(R.id.btnShowAnswer);
        btnYes = (Button)findViewById(R.id.btnYes);
        btnNo = (Button)findViewById(R.id.btnNo);
        
        txtName = (TextView)findViewById(R.id.txtName);
        
        txtQuestionHead = (TextView)findViewById(R.id.txtQuestionHead);
        txtQuestion = (TextView)findViewById(R.id.txtQuestion);
        txtAnswer = (TextView)findViewById(R.id.txtAnswer);
        txtAnswerHead = (TextView)findViewById(R.id.txtAnswerHead);
        txtAnsweredQuestion = (TextView)findViewById(R.id.txtAnsweredQuestion);
        
        hideAnswer();
        
        btnShowAnswer.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showAnswer();
			}
		});
        
        btnNo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pressedNo();				
			}
		});
        
        btnYes.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pressedYes();				
			}
		});
        
        test = Test.getStartTest();
        
        txtName.setText(test.getName());
        
        fillTextViews();
    }
	
	protected void showAnswer(){
		btnShowAnswer.setVisibility(View.INVISIBLE);
		btnYes.setVisibility(View.VISIBLE);
		btnNo.setVisibility(View.VISIBLE);
		txtAnswerHead.setVisibility(View.VISIBLE);
		txtAnswer.setVisibility(View.VISIBLE);
		txtAnsweredQuestion.setVisibility(View.VISIBLE);
	}
	
	protected void hideAnswer(){
		btnShowAnswer.setVisibility(View.VISIBLE);
		btnYes.setVisibility(View.INVISIBLE);
		btnNo.setVisibility(View.INVISIBLE);
		txtAnswerHead.setVisibility(View.INVISIBLE);
		txtAnswer.setVisibility(View.INVISIBLE);
		txtAnsweredQuestion.setVisibility(View.INVISIBLE);
	}
	
	protected void pressedNo(){
		test.getNotAnsweredQuestion().remove(0);
		fillTextViews();
		hideAnswer();
		if(question != null) question.answerOnQuestion(false);
	}
	
	protected void pressedYes(){
		test.getNotAnsweredQuestion().remove(0);
		fillTextViews();
		hideAnswer();
		if(question != null) question.answerOnQuestion(true);
	}
	
	protected void fillTextViews(){
		if(test.getNotAnsweredQuestion().size() == 0){
			test.setStarted(false);
			Intent intent = new Intent(TestActivity.this, StatisticActivity.class);
			startActivity(intent);
			finish();
			return;
		}
		
		int idx = test.getNotAnsweredQuestion().get(0);
		this.question = test.getQuestions().get(idx);
		txtQuestion.setText(question.getQuestion());
		txtAnswer.setText(question.getAnswer());
		
		Integer count = test.getQuestions().size() - test.getNotAnsweredQuestion().size() + 1;
		txtQuestionHead.setText(getString(R.string.question) + " " + count + "/" + test.getQuestions().size());
	}
}
